﻿using System;
using System.Collections.Generic;
using System.Text;
using AMS.DataAccess.Interfaces;
using AMS.DataAccess.BO;
using AMS.DataAccess.Common;
using System.Diagnostics;

namespace AMS.DataAccess.Sql
{
    public class ContactorManager : IContactorManager
    {
        private readonly string ContactorTable = "TC_Contactor";
        #region IContactorManager Members

        public List<ContactorInfo> GetCustomerContactors(CallContext context, int customerId)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT * FROM TC_Contactor");
            sql.Add("WHERE");
            sql.Add("    ", new SqlCondition("CustomerID", customerId));
            sql.Add("ORDER BY");
            sql.Add("    isFirst DESC");
#if SHOW_SQL
            Debug.WriteLine(sql.ToString());
#endif
            var ds = SqlDBHelper.Query(sql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                var list = BusinessObjectMapping.DataTableToBOList<ContactorInfo>(ds.Tables[0]);
                return list;
            }
            else
            {
                return null;
            }
        }

        public ContactorInfo GetCustomerFirstContactor(CallContext context, int customerId)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT TOP 1 * FROM TC_Contactor");
            sql.Add("WHERE");
            sql.Add("    ", new SqlCondition("CustomerID", customerId));
            sql.Add("ORDER BY");
            sql.Add("    isFirst DESC");
#if SHOW_SQL
            Debug.WriteLine(sql.ToString());
#endif
            var ds = SqlDBHelper.Query(sql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                var contactor = BusinessObjectMapping.DataRowToBO<ContactorInfo>(ds.Tables[0].Rows[0]);
                return contactor;
            }
            else
            {
                return null;
            }
        }

        public int UpdateContactors(CallContext context, IList<ContactorInfo> contactors)
        {
            if (contactors == null || contactors.Count == 0)
            {
                throw new ArgumentException("The argument contactors cannot be null or empty.");
            }

            SqlBatch batch = new SqlBatch();
            foreach (ContactorInfo contactor in contactors)
            {
                SqlUpdate update = batch.CreateSqlUpdate(ContactorTable);
                update.AddBusinessObject(contactor, typeof(ContactorInfo));
                update.AddCondition(new SqlCondition("AID", contactor.AID));
            }
#if SHOW_SQL
            Debug.WriteLine(batch.ToString());
#endif
            string sql = batch.ToString();
            return SqlDBHelper.ExecuteSql(sql);
        }

        public int AddContactors(CallContext context, IList<ContactorInfo> contactors)
        {
            if (contactors == null || contactors.Count == 0)
            {
                throw new ArgumentException("The argument contactors cannot be null or empty.");
            }

            SqlBatch batch = new SqlBatch();
            foreach (ContactorInfo contactor in contactors)
            {
                SqlInsert update = batch.CreateSqlInsert(ContactorTable);
                update.AddBusinessObject(contactor, typeof(ContactorInfo));
            }
#if SHOW_SQL
            Debug.WriteLine(batch.ToString());
#endif
            string sql = batch.ToString();
            return SqlDBHelper.ExecuteSql(sql);
        }

        #endregion

        public bool DeleteContactor(CallContext context, int contactorId)
        {
            SqlDelete delete = new SqlDelete("TC_Contactor");
            delete.AddCondition(new SqlCondition("AID", contactorId));
#if SHOW_SQL
            Debug.WriteLine(delete.ToString());
#endif
            return 0 < SqlDBHelper.ExecuteSql(delete.ToString());
        }

        public void AddContactor(CallContext context, ContactorInfo contactor)
        {
            SqlInsert insert = new SqlInsert("TC_Contactor");
            insert.AddBusinessObject(contactor);
            insert.Add("SELECT @@IDENTITY");
#if SHOW_SQL
            Debug.WriteLine(insert.ToString());
#endif
            var ds = SqlDBHelper.Query(insert.ToString());
            contactor.AID = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
        }
    }
}
